<template>
	<div>
		<navigation-bar>
			<div class="searchContent">
				<el-input v-model="pmName" placeholder="请输入项目名称" size="mini"
					style="width: 150px; margin-right: 10px"></el-input>
				<el-input v-model="pmNum" placeholder="请输入项目编号" size="mini"
					style="width: 150px; margin-right: 10px"></el-input>
				<custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
				<custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>
				<custom-button type="add" @addClick="addClick"></custom-button>
			</div>
		</navigation-bar>
		<table-index :dataSource="dataSource" :tableColumns="tableColumns" :btnList="btnList" :pageSize="pageSize"
			:pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" @editRow="editRow" @viewRow="viewRow"
			@deleteRow="deleteRow"></table-index>

		<div v-if="dialogShow">
			<custom-dialog :title="dialogTitle" size="small" @dialogClose="dialogClose">
				<project-information-form :type="type" :data="data" @projectSubmit="submit"></project-information-form>
			</custom-dialog>
		</div>
	</div>
</template>

<script>
import { ProjectInformationFormConfig } from "@/assets/form/managementCenter/projectManage/projectInformationForm"
import customDialog from "@/components/customComponents/customDialog"
import customButton from "@/components/customComponents/customForm/customButton"
import tableIndex from "@/components/customComponents/customeTable/tableIndex"
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import { AjaxCommon } from "@/libs/apiCommon"
import { PopupCommon } from "@/libs/popupCommon"
import { SessionCommon } from "@/libs/sessionCommon"
import pubsub from "pubsub-js"
import projectInformationForm from "./projectInformationForm"
export default {
	components: {
		navigationBar,
		customButton,
		tableIndex,
		customDialog,
		projectInformationForm
	},
	data() {
		return {
			// 申办方列表
			sponsorData: [],
			// CRO列表
			croData: [],
			pmName: "",
			pmNum: "",
			// table 相关参数  begin
			dataSource: [],
			tableColumns: [
				{
					prop: "pmName",
					label: "项目名称",
				},
				{
					prop: "pmNum",
					label: "项目编号",
				},
				{
					prop: "pmSimpleName",
					label: "项目简称",
				},
				{
					prop: "filingsNum",
					label: "批件号/备案号",
				},
				{
					prop: "centerNum",
					label: "本中心编号",
				},
				{
					prop: "planNum",
					label: "方案编号",
				},
				{
					prop: "innerPlanNum",
					label: "内部编号",
				},
			],
			btnList: ["edit", "view", "delete"],
			pageSize: 20,
			pageCount: 100,
			pageNo: 1,
			// table 相关参数  end

			// 弹窗是否显示
			dialogShow: false,
			// 弹窗标题 (新增修改不同)
			dialogTitle: "",

			// form的配置信息
			formConfig: ProjectInformationFormConfig,
			type: "add",
			data: {},
		}
	},

	methods: {
		submit(d) {
			let self = this
			console.log(self, d)
			self.dialogShow = false
			//  需要去页面：projectInformationForm去改变字段的值，研究者信息和版本信息的字段我乱填的
			// PopupCommon.notify("info", "需要去页面：projectInformationForm去改变字段的值，研究者信息和版本信息的字段我乱填的，改完后，把下面的代码放开即可。")
			if (d.id) {
				AjaxCommon("/ccdc/cfg/pm/pmProject/edit", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							self.initPage()
							self.dialogClose()
							pubsub.publish("projectChange", "修改了项目信息")
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			} else {
				delete d.id
				AjaxCommon("/ccdc/cfg/pm/pmProject/add", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							self.initPage()
							self.dialogClose()
							pubsub.publish("projectChange", "新增了项目信息")
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			}
		},
		dialogClose() {
			this.dialogShow = false
		},
		searchClick() {
			this.pageNo = 1
			this.initPage()
		},
		resetClick() {
			this.pmName = ""
			this.pmNum = ""
		},
		addClick() {
			let self = this
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res === "fieldName") {
						if (t[res] === "sponsorId") {
							t.itemList = self.sponsorData
							t.filedValue = ""
						} else if (t[res] === "croId") {
							t.itemList = self.croData
							t.filedValue = ""
						} else if (t[res] === "centerNum") {
							t.filedValue = "01"
						} else {
							t.filedValue = ""
						}
					}
				})
			})
			this.formConfig = formConfigItem
			this.type = "add"
			this.dialogShow = true
			this.dialogTitle =
				"新增" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		// 分页点击事件
		pageChange(p) {
			this.pageNo = p
			this.initPage()
		},
		editRow(d) {
			this.type = "edit"
			// console.log(d.data,'d.data');
			this.data = d.data
			// console.log(d)
			this.dialogShow = true
			this.dialogShow = true
			this.dialogTitle =
				"编辑" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		viewRow(d) {
			this.type = "look"
			this.data = d.data
			this.dialogShow = true
			this.dialogTitle =
				"查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		deleteRow(d) {
			const id = d.data.id
			let self = this
			AjaxCommon("/ccdc/cfg/pm/pmProject/delete?id=" + id, "delete", { optReason: d.optReason }).then(
				(res) => {
					console.log(res, "res...............")
					if (res.data.code == 200) {
						PopupCommon.notify("success", "删除成功")
						self.initPage()
						self.dialogClose()
						pubsub.publish("projectChange", "删除了项目信息")
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				}
			)
		},
		// 初始化事件
		initPage() {
			let self = this
			AjaxCommon("/ccdc/cfg/pm/pmProject/listByPage", "post", {
				pmName: this.pmName,
				pmNum: this.pmNum,
				pageNo: this.pageNo,
				pageSize: this.pageSize,
			}).then((res) => {
				if (res.data.code == 200) {
					self.dataSource = res.data.result.records
					self.pageCount = res.data.result.total
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			})
		},
		getType() {
			const self = this
			// 查询申办方下拉列表
			AjaxCommon("/ccdc/cfg/pm/pmSponsor/listByPage", "post", {
				pageSize: "100000",
			}).then((res) => {
				if (res.data.code == 200) {
					res.data.result.records.forEach((e) => {
						var sponsor = {}
						sponsor.label = e.pmName
						sponsor.value = e.id
						self.sponsorData.push(sponsor)
					})
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			})
			// 查询cro下拉列表
			AjaxCommon("/ccdc/cfg/pm/pmCro/listByPage", "post", {
				pageSize: "100000",
			}).then((res) => {
				if (res.data.code == 200) {
					res.data.result.records.forEach((e) => {
						var cro = {}
						cro.label = e.corName
						cro.value = e.id
						self.croData.push(cro)
					})
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			})
		},
	},
	mounted() {
		this.initPage()
		this.getType()
	},
}
</script>

<style></style>
